package com.note.service.note;

import com.note.dao.note.noteDao;
import com.note.dao.note.noteDaoImpl;
import com.note.pojo.Note;
import com.note.pojo.Notetype;
import com.note.pojo.notePageShow;
import com.note.pojo.page;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

public class noteServiceImpl implements noteService{

    @Override
    public boolean writeNote(String title, String content, int typeId, Timestamp pubtime, int userId) throws SQLException {
        noteDao write = new noteDaoImpl();
        boolean flag = write.write(title, content, typeId, pubtime, userId);
        return flag;
    }

    @Override
    public page allNote(int currentPage, int pageSize) throws SQLException {
        noteDao note = new noteDaoImpl();
        int all = note.all();
        page page = new page();
        page.setCurrentPage(currentPage);
        page.setTotalData(all);
        page.setPageSize(pageSize);
        List<notePageShow> notePageShows = note.showAllNote(((currentPage-1)*pageSize), pageSize);
        page.setNotes(notePageShows);
        return page;
    }

    @Override
    public boolean delete(int noteId) throws SQLException {
        noteDao d = new noteDaoImpl();
        int i = d.deleteNote(noteId);
        if(i>0){
            return true;
        }else
            return false;
    }

    @Override
    public HashMap edit(int noteId) throws SQLException {
        noteDao a = new noteDaoImpl();
        Note note = a.selectOne(noteId);
        List<Notetype> typeList = a.getTypeList();
        HashMap map = new HashMap();
        map.put("note", note);
        map.put("typeList", typeList);
        return map;
    }

    @Override
    public boolean updateNote(String title, String content, int typeId, int noteId) throws SQLException {
        noteDao update = new noteDaoImpl();
        int i = update.updateNote(title, content,typeId, noteId);
        if (i>0)
            return true;
        else
            return false;
    }

    @Override
    public List<Notetype> getTypeList() throws SQLException {
        noteDao type = new noteDaoImpl();
        List<Notetype> typeList = type.getTypeList();
        return typeList;
    }

    @Override
    public List<notePageShow> getNoteByType(int typeId) throws SQLException {
        noteDao n = new noteDaoImpl();
        List<notePageShow> noteByType = n.getNoteByType(typeId);
        return noteByType;
    }

    @Override
    public List<notePageShow> getNoteByTitle(String title) throws SQLException {
        noteDao n = new noteDaoImpl();
        List<notePageShow> noteBytitle = n.getNoteBytitle(title);
        return noteBytitle;
    }

    @Override
    public boolean deleteTypeById(int id) throws SQLException {
        noteDao n = new noteDaoImpl();
        int i = n.deleteTypeById(id);
        if(i>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean addType(String typeName) throws SQLException {
        noteDao n = new noteDaoImpl();
        int i = n.addType(typeName);
        if(i>0){
            return true;
        }else {
            return false;
        }
    }
}
